<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="page" tagdir="/WEB-INF/tags"%>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Topic List</title>
    <link rel="stylesheet" type="text/css" href="<c:url value="/CSS/topic/topicList.css"/>"/>
  </head>
  <body>
    <div class="container">
      <jsp:include page="/WEB-INF/JSP/head.jsp"/>
      <div class="body">
        <div class="topic-list">
          <div class="caption">
            <div class="title">
              <a href="<c:url value="${sessionScope.boardPathURL}"/>">&lt;==</a>&nbsp;Topic List - ${board.boardName}
            </div>
            <div class="add-topic">
              <c:choose>
                <c:when test="${sessionScope.sessionUser != null}">
                  <a href="javascript:" onclick="javascript:openWindow('<c:url value="/topic/addTopic-${board.boardId}"/>', 600, 320);">Add Topic</a>
                </c:when>
                <c:otherwise>
                  <span class="grey">Add Topic</span>
                </c:otherwise>
              </c:choose>
            </div>
          </div>
          <div class="tr1">
            <div class="td1">Title</div>
            <div class="td2">Author</div>
            <div class="td3">Views</div>
            <div class="td4">Replies</div>
            <div class="td5">Digest</div>
            <div class="td6">CreatedTime</div>
            <div class="td7">Update</div>
            <div class="td8">Delete</div>
          </div>
          <c:if test="${pagedTopics.list != null}">
            <c:forEach var="topic" items="${pagedTopics.list}" varStatus="status">
              <div class="tr2" id="row_${status.index}"
                onmouseover="toggleBackground('row_${status.index}', 0)"
                onmouseout="toggleBackground('row_${status.index}', 1)">
                <div class="td1"><a href="<c:url value="/post/postList-${topic.topicId}"/>">${topic.title}</a></div>
                <div class="td2">${topic.user.userName}</div>
                <div class="td3">${topic.views}</div>
                <div class="td4">${topic.posts.size()}</div>
                <div class="td5">
                  <script type="text/javascript">
                    function getFields(checked, topicId, boardId) {
                      var str = checked + '-' + topicId + '-' + boardId;
                      window.location.href = "<c:url value='/topic/topicList/" + str + "'/>";
                    }
                  </script>
                  <c:if test="${topic.digest == 0 }">
                    <c:if test="${boardManager != null}">
                      <input type="checkbox" onchange="javascript:getFields(0, ${topic.topicId}, ${board.boardId})"/>
                    </c:if>
                    <c:if test="${boardManager == null}">
                      <input type="checkbox" disabled="disabled" onchange="javascript:getFields(0, ${topic.topicId}, ${board.boardId})"/>
                    </c:if>
                  </c:if>
                  <c:if test="${topic.digest != 0 }">
                    <c:if test="${boardManager != null}">
                      <input type="checkbox" checked="checked" onchange="javascript:getFields(1, ${topic.topicId}, ${board.boardId})"/>
                    </c:if>
                    <c:if test="${boardManager == null}">
                      <input type="checkbox" disabled="disabled" checked="checked" onchange="javascript:getFields(1, ${topic.topicId}, ${board.boardId})"/>
                    </c:if>
                  </c:if>
                </div>
                <div class="td6">${fn:substring(topic.createTime, 0, 19)}</div>
                <div class="td7">
                  <c:choose>
                    <c:when test="${sessionScope.sessionUser != null && sessionScope.sessionUser.userId == topic.user.userId}">
                      <a href="javascript:" onclick="javascript:openWindow('<c:url value="/topic/updateTopic-${topic.topicId}"/>', 600, 320)">Update</a>
                    </c:when>
                    <c:otherwise>
                      <span class="grey">Update</span>
                    </c:otherwise>
                  </c:choose>
                </div>
                <div class="td8">
                  <c:choose>
                    <c:when test="${boardManager != null || (sessionScope.sessionUser != null && sessionScope.sessionUser.userId == topic.user.userId)}">
                      <a href="javascript:" onclick="javascript:openWindow('<c:url value="/topic/deleteTopic-${topic.topicId}"/>', 600, 320)">Delete</a>
                    </c:when>
                    <c:otherwise>
                      <span class="grey">Delete</span>
                    </c:otherwise>
                  </c:choose>
                </div>
              </div>
            </c:forEach>
          </c:if>
          <div id="page">
            <c:choose>
              <c:when test="${boardId != null}">
                <page:PageBar pageURL="/topic/topicList-${boardId}" pageAttrKey="pagedTopics"></page:PageBar>
              </c:when>
              <c:when test="${(boardId == null) && (board.boardId != null)}">
                <page:PageBar pageURL="/topic/topicList-${board.boardId}" pageAttrKey="pagedTopics"></page:PageBar>
              </c:when>
            </c:choose>
          </div>
        </div>
      </div>
      <jsp:include page="/WEB-INF/JSP/foot.jsp"/>
    </div>
    <script type="text/javascript" src="<c:url value="/JS/jquery-1.7.2.min.js"/>"></script>
    <script type="text/javascript" src="<c:url value="/JS/jcommon.js"/>"></script>
  </body>
</html>